unit EMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, GraphicEx, Buttons, XPMan, jpeg, DB,
  ZAbstractRODataset, ZAbstractDataset, ZDataset, Menus;

type
  TsbWindow=class(TSpeedButton)
  private
    Ffmproperty: HWND;
    Ffmname: string;
  public

  published
    property fmClientHandle: HWND read Ffmproperty write Ffmproperty;
    property fmname:string read Ffmname write Ffmname;
  end;

type
  TfmMain = class(TForm)
    pnAppTitle: TPanel;
    pnAppBar: TPanel;
    imgLogo: TImage;
    lblCompanyName: TLabel;
    Panel3: TPanel;
    sbTark: TScrollBox;
    XPManifest1: TXPManifest;
    SpeedButton1: TSpeedButton;
    imgManiMenu: TImage;
    Panel1: TPanel;
    Image3: TImage;
    qrConfig: TZQuery;
    lblShow: TLabel;
    GroupBox1: TGroupBox;
    Image1: TImage;
    Image2: TImage;
    Image4: TImage;
    Image5: TImage;
    Image6: TImage;
    Image7: TImage;
    Image8: TImage;
    Image9: TImage;
    Image10: TImage;
    Panel2: TPanel;
    imgLanguage: TImage;
    pnuLanguage: TPopupMenu;
    hai1: TMenuItem;
    English1: TMenuItem;
    procedure FormActivate(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure imgManiMenuClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure Image3Click(Sender: TObject);
    procedure Image4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Image5Click(Sender: TObject);
    procedure imgLanguageClick(Sender: TObject);
    procedure hai1Click(Sender: TObject);
    procedure English1Click(Sender: TObject);
  private
    { Private declarations }
    procedure ArrangeQuickButtons;
    procedure sbQuick(sender:tobject);
    procedure sbDown(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
    procedure showLogo;
    Procedure LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
    procedure refresh_caption;
    procedure changcaption;

  public
    { Public declarations }
    procedure add2sbwin(nofm: HWND;cap,nameform:string);
    procedure delete2sbwin(nofm: HWND);
    procedure FindButtonQuick(nofm: HWND);
    function Find2FmExist(sName: string): boolean;
  end;

var
  fmMain: TfmMain;

implementation

uses EUser, ETask, DLoading, EConfig, dmIdocqment, EPayment, EUserGroup,
  Utility;

{$R *.dfm}

procedure TfmMain.FormActivate(Sender: TObject);
begin
  fmMain.pnAppBar.Parent := TForm(Sender);
  fmMain.pnAppTitle.Parent := TForm(Sender);
  fmMain.lblShow.Caption:= '';
end;

procedure TfmMain.add2sbwin(nofm: HWND; cap, nameform: string);
var sb: TsbWindow;
begin
  sb:=TsbWindow.create(self);
  try
    sb.Ffmproperty:=nofm;
    sb.fmname:=nameform;
    sb.caption:=cap;
    sb.hint:=Cap;
    //sb.GroupIndex := 2;
    //sb.Margin := 3;
    //sb.Width := 90;

    sb.GroupIndex := 2;
    sb.Margin := 3;
    sb.Width := 90;
    sb.Height:=75;

    sb.parent:=sbTark;
    sb.down:=true;
    //sb.Glyph.LoadFromResourceName(HInstance,'Form');
    //sb.numGlyphs:=1;
    //sb.flat:=true;
    sb.onClick:=sbQuick;
    sb.onMouseDown:=sbDown;
    //sb.popupmenu:=popClose;
    //sb.onMouseMove:=sbMouseMove;
    sb.ShowHint:=true;
    sb.Font.Size:=12;
    sb.Font.Style:=[fsBOLD];

    ArrangeQuickButtons;
  finally
  end;
end;

procedure TfmMain.delete2sbwin(nofm: HWND);
var
  j : Integer;
  //snd_:string;
  fm:TfmLoading;
begin
  //lblShow.caption:='';
  for j := 0 to sbTark.ControlCount -1  do
  begin
    if sbTark.Controls[j] is TsbWindow then
      if TsbWindow(sbTark.Controls[j]).fmClientHandle= nofm then
      begin
        Application.ProcessMessages;
        TsbWindow(sbTark.Controls[j]).Free;
        Break;

      end;
  end ;
  ArrangeQuickButtons;

end;

function TfmMain.Find2FmExist(sName: string): boolean;
var
  j : Integer;
  cm:TComponent;
begin
  result:=false;
  for j := 0 to sbTark.ControlCount -1  do
  begin
    cm:=sbTark.Controls[j];
    if cm is TsbWindow then
      result:= TsbWindow(cm).fmname=  sName;
      if result then
      begin
        sbQuick(cm);
        Break;
      end ;
  end ;
end;

procedure TfmMain.FindButtonQuick(nofm: HWND);
var
  j : Integer;
begin
  for j := 0 to sbTark.ControlCount -1  do
  begin
    if sbTark.Controls[j] is TsbWindow then
      if TsbWindow(sbTark.Controls[j]).fmClientHandle=  nofm then
      begin
        TsbWindow(sbTark.Controls[j]).down:=true;
        lblShow.caption:=TsbWindow(sbTark.Controls[j]).Hint + '       ';
        //lblShow.Repaint;
        //show_active_form(TsbWindow(sbTark.Controls[j]).Hint);
        Break;
      end ;
  end ;
end;

procedure TfmMain.ArrangeQuickButtons;
const
  MaxWidth = 90;
  MinWidth = 50;
var
  i,QWidth,TheLeft,NewWidth,TheTop : Integer;
begin
  if sbTark.ControlCount > 0 then
    sbTark.ScrollInView(sbTark.Controls[0]);
  QWidth := sbTark.Width-10;
  TheLeft := 0;

  for i :=0 to sbTark.ControlCount - 1 do
      TheLeft := TheLeft + sbTark.Controls[i].Width;

  if TheLeft > QWidth then
  begin
    NewWidth := QWidth div sbTark.ControlCount;
    If NewWidth < MinWidth Then NewWidth := MinWidth;
  end else
    NewWidth := MaxWidth;

  TheLeft := 0;
  TheTop  := 0;
  for i :=0 to sbTark.ControlCount - 1 do
  begin
    if TheLeft + NewWidth > QWidth then
    begin
     TheTop := TheTop + sbTark.Controls[i].Height +1;
     TheLeft := 0;
    end;
    if NewWidth = MinWidth then
      TsbWindow(sbTark.Controls[i]).Caption := COPY(TsbWindow(sbTark.Controls[i]).Hint,1,2)+'...'
    else
      TsbWindow(sbTark.Controls[i]).Caption := COPY(TsbWindow(sbTark.Controls[i]).Hint,1,10)+'...';

    sbTark.Controls[i].Left := TheLeft;
    sbTark.Controls[i].Top := TheTop;
    sbTark.Controls[i].Width := NewWidth;
    TheLeft := TheLeft + NewWidth;
  end;
end;

procedure TfmMain.sbDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
  begin
    sbQuick(sender)
  end;
end;

procedure TfmMain.sbQuick(sender: tobject);
var i:integer;
begin
  for i := 0 to Screen.FormCount - 1 do
  begin
    if Screen.Forms[i].Handle = TsbWindow(Sender).fmClientHandle then
    begin
      if Screen.Forms[i].CanFocus then
      begin
        if Screen.Forms[i].name = 'fmPayment' then
        begin
          Image2Click(sender);
        end else
        if Screen.Forms[i].name = 'fmTask' then
        begin
          Image1Click(Sender);
        end else
        if Screen.Forms[i].name = 'fmConfig' then
        begin
          Image4Click(Sender);
        end else
        if Screen.Forms[i].name = 'fmUserGroup' then
        begin
          Image5Click(Sender);
        end;

        Break;
      end;
    end;
  end;
end;

procedure TfmMain.Image1Click(Sender: TObject);
begin
  if not Assigned(fmTask) then
    fmTask := TfmTask.Create(Self);
  fmTask.Show;
  fmTask.BringToFront;
end;

procedure TfmMain.imgManiMenuClick(Sender: TObject);
begin
  fmMain.BringToFront;
end;

procedure TfmMain.FormCreate(Sender: TObject);
begin
  Width:=Screen.Width;
  Height:=Screen.Height -40;
  Left:=1;
  Top := 0;

  Application.ProcessMessages;
end;

procedure TfmMain.SpeedButton1Click(Sender: TObject);
begin
  imgManiMenuClick(Sender);
end;

procedure TfmMain.Image3Click(Sender: TObject);
begin
  Close;
end;

procedure TfmMain.Image4Click(Sender: TObject);
begin
  if not Assigned(fmConfig) then
    fmConfig := TfmConfig.Create(Self);
  fmConfig.Show;
  fmConfig.BringToFront;
end;

procedure TfmMain.showLogo;
var
  bmp: TBitmap;
begin
  bmp := TBitmap.Create;
  try
    LoadBitmapFromBlob(bmp, TBlobField(qrConfig.FieldByName('pict')));
    imgLogo.Picture.Assign(bmp);
    //bmp.SaveToFile(OpenDialog.FileName);
    //imgManiMenu.Picture.Assign(bmp);
    lblCompanyName.caption:=qrConfig.fieldbyname('compname').asstring;
  finally
    bmp.Free;
  end;
end;

procedure TfmMain.FormShow(Sender: TObject);
var FileName:string;
begin
  qrConfig.Open;
  showLogo;

  imgLanguage.Picture.Assign(nil);
  if dm.languagedef='TH' then
    FileName:=ExtractFilePath(Application.ExeName)+ 'Resource\ThaiLand.bmp'
  else
    FileName:=ExtractFilePath(Application.ExeName)+ 'Resource\English.bmp';

  if FileExists(FileName) then
    imgLanguage.Picture.LoadFromFile(FileName);
  
end;

procedure TfmMain.LoadBitmapFromBlob(Bitmap: TBitmap; Blob: TBlobField);
var
  ms, ms2: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  try
    Blob.SaveToStream(ms);
    ms.Position := 0;
    Bitmap.LoadFromStream(ms);

  finally
    ms.Free;
  end;
end;

procedure TfmMain.Image2Click(Sender: TObject);
begin
  if not Assigned(fmPayment) then
    fmPayment := TfmPayment.Create(Self);
  fmPayment.Show;
  fmPayment.BringToFront;
end;

procedure TfmMain.Image5Click(Sender: TObject);
begin  
  if not Assigned(fmUserGroup) then
    fmUserGroup := TfmUserGroup.Create(Self);
  fmUserGroup.Show;
  fmUserGroup.BringToFront;
end;

procedure TfmMain.imgLanguageClick(Sender: TObject);
begin
  pnuLanguage.Popup(imgLanguage.ClientOrigin.X,imgLanguage.ClientOrigin.Y - 40);
end;

procedure TfmMain.hai1Click(Sender: TObject);
var FileName:string;
begin
  dm.languagedef:='TH';
  imgLanguage.Picture.Assign(nil);
  FileName:=ExtractFilePath(Application.ExeName)+ 'Resource\ThaiLand.bmp';
  if FileExists(FileName) then
    imgLanguage.Picture.LoadFromFile(FileName);

  refresh_caption;
  changcaption;
end;

procedure TfmMain.English1Click(Sender: TObject);
var FileName:string;
begin
  dm.languagedef:='EN';
  imgLanguage.Picture.Assign(nil);
  FileName:=ExtractFilePath(Application.ExeName)+'Resource\English.bmp';
  if FileExists(FileName) then
    imgLanguage.Picture.LoadFromFile(FileName);
    
  refresh_caption;
  changcaption;
end;

procedure TfmMain.changcaption;
begin

end;

procedure TfmMain.refresh_caption;
var i :integer;
begin
  for i:=0 to Screen.FormCount - 1 do
  begin
    PostMessage(Screen.Forms[i].Handle, QEventType_CMSetLang, 0, 0);
  end;
end;

end.
